package com.opt.sample;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

public class Algorithm {
	private static Logger LOGGER = Logger.getLogger(Algorithm.class);

	private int algorithmCycles;
	private int populationSize;
	private int valueOfPackages;

	NewPopulation POPULATION;

	public Algorithm(int cycles, String filepath, int populationSize,
			int valueOfPackages) {
		LOGGER.debug("Algorithm started...");
		this.populationSize = populationSize;
		this.valueOfPackages = valueOfPackages;
		this.algorithmCycles = cycles;
		WorkOnAlgorithm won = new WorkOnAlgorithm(filepath);
		BestGene bestGene = new BestGene();
		List<NewTask> tasks;

		for (int i = 0; i < cycles; i++) {

			if (i == 0) {
				POPULATION = won
						.initPopulation(populationSize, valueOfPackages);
			}
			bestGene = won.getTheBestGene(POPULATION, 10);
			tasks = won.crossTask(bestGene, POPULATION);

			for (NewTask task : tasks) {
				won.mixTask(task, 10);
			}

			POPULATION.setTasks(tasks);
			Collections.sort(POPULATION.tasks);
			won.setIterationToExcel(i + 1, POPULATION, filepath);
			LOGGER.debug("cycle: " + i);
		}
		won.closeFile();
		System.exit(0);
	}

}
